//
//  NShenbaoTypeViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/22.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NShenbaoTypeViewController.h"
#import "NShenbaoTypeCell.h"
#import "NShenbaoTypeDetailViewController.h"
#import "NShenbaoTypeModel.h"
@interface NShenbaoTypeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@end

#define cellIdentifier @"shenbaoType"

@implementation NShenbaoTypeViewController

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64-45) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 100.f;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NShenbaoTypeCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//                        [ws loadData];
//        }];
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    self.dataArray = [NSMutableArray array];
    [self.navigationItem setItemWithTitle:@"项目申报类型" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    [self.view addSubview:self.tableView];
    [self loadData];
    // Do any additional setup after loading the view.
}


- (void)loadData{
    [[NNRCNetRequest manager]getWithUrl:kShenbaoTypelistUrl params:nil success:^(id result) {
        YYLog(@"%@",result);
        WeakStament(ws);
        if ([[result objectForKey:@"code"] integerValue] == 0) {

            NSArray *arr = result[@"page"][@"records"];
            YYLog(@"%@",arr);
            for (NSDictionary *dict in arr) {
                NShenbaoTypeModel *model = [NShenbaoTypeModel mj_objectWithKeyValues:dict];
                [ws.dataArray addObject:model];
            }
            [ws.tableView reloadData];

        }else{
            [AFToast showText:result[@"msg"]];
        }
        
    } failure:^(id failure) {
        
    }];
}
#pragma mark - delegate  datasource

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NShenbaoTypeModel *model = self.dataArray[indexPath.row];
    NShenbaoTypeDetailViewController *detail =  [NShenbaoTypeDetailViewController new];
    if (model.projectDeclarationId) {
        detail.shenbaoId = model.projectDeclarationId;
    }
    
    [self.navigationController pushViewController:detail animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.dataArray.count;
//    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NShenbaoTypeModel *model = self.dataArray[indexPath.row];
    NShenbaoTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (self.dataArray.count>indexPath.row) {
        cell.model = model;
    }
    return cell;
}

@end
